var express = require('express');
var pool = require('../pool');
var router = express.Router();

/* GET home page. */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });

// 查询分类的接口
router.get('/category', (req, res) => {
  let sql = 'SELECT * FROM menu_typetbi';
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200, results: results });
  });
});
module.exports = router;

//查询菜谱的接口
router.get('/detail', (req, res) => {
  let id = req.query.cid;
  // SQL查询
  // let sql = "SELECT r.tid,subject,content,created_at,nickname,avatar,article_number FROM xzqa_article AS r INNER JOIN menu_cookbook AS u ON category_id = u.id WHERE r.id=?";
  let sql = "select * from menu_cookbook where category_id=?"
  // 执行SQL查询
  pool.query(sql, [id], (error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    if (results[0]) {
      res.send({ message: 'ok', code: 200, result: results });
    }else{
      res.send({
        msg:"未查询到",
        code:201
      })
    }
  });
});

// 查询所有菜品
router.get('/hot', (req, res) => {
  // SQL查询
  let id = req.query.cid;
  // let sql = "SELECT r.tid,subject,content,created_at,nickname,avatar,article_number FROM xzqa_article AS r INNER JOIN menu_cookbook AS u ON category_id = u.id WHERE r.id=?";
  let sql = "select * from menu_cookbook where cid=?" 
  // 执行SQL查询
  pool.query(sql,[id],(error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    if (results[0]) {
      res.send({ message: 'ok', code: 200, result: results });
      return;
    }else{
      res.send({
        msg:"未查询到",
        code:201
      })
    }
  });
});
//查询菜谱配料
router.get('/ingredient', (req, res) => {
  let id = req.query.cid;
  let sql = "select * from menu_ingredient where food_id=?"
  // 执行SQL查询
  pool.query(sql, [id], (error,results) => {
    if (error) throw error;
    // 返回数据到客户端
    if (results[0]) {
      res.send({ message: 'ok', code: 200, result: results });
      return;
    }else{
      res.send({
        msg:"未查询到",
        code:201
      })
    }
  });
});

//查询菜谱步骤
router.get('/step', (req, res) => {
  let id = req.query.cid;
  let sql = "select * from menu_step where food_id=?"
  // 执行SQL查询
  pool.query(sql, [id], (error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    if (results[0]) {
      res.send({ message: 'ok', code: 200, result: results });
    }else{
      res.send({
        msg:"未查询到",
        code:201
      })
    }
  });
});

//查询用户的评论
router.get('/comm', (req, res) => {
  let id = req.query.cid;
  let sql = "select * from menu_comm where food_id=?"
  // 执行SQL查询
  pool.query(sql, [id], (error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    if (results[0]) {
      res.send({ message: 'ok', code: 200, result: results });
    }else{
      res.send({
        msg:"未查询到",
        code:201
      })
    }
  });
})

// 搜索关键字
router.get('/keyword', (req, res) => {
  let key = req.query.key;
  console.log(key)
  // 执行SQL查询
  pool.query(`select * from menu_cookbook where dishes like '%${key}%'`, (error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    if (results[0]) {
      res.send({ message: 'ok', code: 200, result: results });
    }else{
      res.send({
        msg:"未查询到",
        code:201
      })
    }
  });
})
